// Copyright  2011, 2012 Petr TALLA. [petr.talla@gmail.com]
//
// This file is free software; you can redistribute it and/or modify it
// under the terms of the GNU Lesser General Public License
// as published by the Free Software Foundation; either
// version 2 of the License, or (at your option) any later version.
//
// This file is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY;  without even the implied warranty
// of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
// See the GNU Lesser General Public License for more details.
// [http://www.gnu.org/copyleft/lesser.html]
//=============================================================================
#include "./TcCmds_htmlView.h"
#include <TcArgCol.h>
#include <TcArgVal.h>
#include <TcHtmlViewTabbed.h>
#include <TcCmdLog.h>
#include <QDir>

//=============================================================================
bool TcCmds_htmlView::registrator_ = TcCmds_htmlView::registerCmds_();

//=============================================================================
TcCmds_htmlView::TcCmds_htmlView()
{
}

//=============================================================================
int TcCmds_htmlView::set_current_tab(TcCmdEngine* /*engine*/, TcArgCol& args)
{   if (args.count() < 1) return args.appendError("tab name must be entered");
    TcArgVal* val = args.at(1)->getAsVal();
    if (val == NULL) return args.appendError("tab name must be value");
    TcHtmlViewTabbed::mainView().tabSetCurrent(val->value());
    return 0;
}

//=============================================================================
int TcCmds_htmlView::tab_set_commands(TcCmdEngine* /*engine*/, TcArgCol& /*args*/)
{
    QString result;

    result.append(

    "<script type='text/javascript'>"


    "function cmd_request() {"
    "    var cmd = document['cmd_form'].elements['cmd'].value;"
    "    window.viewxx.execute(cmd);"
    "    document['cmd_form'].elements['cmd'].value = '';"
    "}"

    "</script>"

    "<div class='input'>"
    "    <form  name='cmd_form' action='/kuku/kuku' method='get'>"
    "        <input size='70'  type='ttab_set_commandsext' name='cmd' onKeyPress='{if (event.keyCode==13)cmd_request()}'>"
    "    </form>"
    "</div>"
    );


    result.append("<div>");


    TcCmdLog* log = TcHtmlViewTabbed::mainView().log_;

    for ( long i = log->count()-1; ( (i > log->count()-11) && (i >=0) ) ; i-- )
    {   result.append(QString("[%0] ").arg(i));
        result.append(log->at(i).cmdRecord());
        if ( log->at(i).isFrontController() == false )
        {   result.append(" [NOT FC: ]");
        }
        result.append("<br>");
    }

    result.append("<p>Code aid:</p>");
    result.append(
        "<a href='tcview:://#load_cmdfile ../macros/ini.tcmd'>load ini</a><br>"
        "<a href='tcview:://#load_cmdfile ../macros/objects.tcmd'>load persons</a><br>"
        "<code>"
        "<b>xobject_create</b> $category|null -name1 $value1 -name2 $value2<br>"
        "<b>xattr_create</b> str id -name $name -value $value<br>"
        "</code>"
        );

    result.append("</div>");


    TcHtmlViewTabbed::mainView().tabSet("commands", result.toAscii().data());
    return 0;
}

//=============================================================================
int TcCmds_htmlView::tab_set_log(TcCmdEngine* /*engine*/, TcArgCol& /*args*/)
{   QString LOG;
    TcCmdLog* log = TcHtmlViewTabbed::mainView().log_;

    for ( long i = 0; i < log->count(); i++ )
    {   if ( log->at(i).isFrontController() == false ) continue;
        LOG.append(QString("[%0]").arg(i));
        LOG.append(log->at(i).cmdRecord());
        LOG.append("<br>");
    }

    TcHtmlViewTabbed::mainView().tabSet("log", LOG.toAscii().data());

    return 0;
}

//=============================================================================
int TcCmds_htmlView::tab_set_info(TcCmdEngine* /*engine*/, TcArgCol& /*args*/)
{   QString result;

    result.append("Current path: ");
    result.append(QDir::currentPath());
    result.append("<br>");

    TcHtmlViewTabbed::mainView().tabSet("info", result.toAscii().data());

    return 0;
}

//=============================================================================

